﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using System.Data.Sql;
using System.IO;

namespace QLDT
{
    public partial class Frm_Backup : DevExpress.XtraEditors.XtraForm
    {
        private SqlConnection conn;
        private SqlCommand command;
        private SqlDataReader reader;
        string sql = "";
        string connectionString = "";
        public Frm_Backup()
        {
            InitializeComponent();
        }

        private void simpleBt_Connect_Click(object sender, EventArgs e)
        {
            try
            {
                connectionString = "Data Source=" + txt_DataSource.Text + ";User Id=" + txt_UserID.Text + ";Password=" + txt_Password.Text + ";";
                conn = new SqlConnection(connectionString);
                conn.Open();
                sql = "EXEC sp_databases";
                // sql = "SELECT * FROM sys.databases d WHERE d.databases_id>4";
                command = new SqlCommand(sql, conn);
                reader = command.ExecuteReader();
                cmbDatabases.Items.Clear();
                while (reader.Read())
                {
                    cmbDatabases.Items.Add(reader[0].ToString());
                }
                txt_DataSource.Enabled = false;
                txt_Password.Enabled = false;
                txt_UserID.Enabled = false;
                simpleBt_Connect.Enabled = false;
                simpleBt_Disconnect.Enabled = true;
                simpleBt_Backup.Enabled = true;
                cmbDatabases.Enabled = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        private void simpleBt_Disconnect_Click(object sender, EventArgs e)
        {
            txt_DataSource.Enabled = true;
            txt_UserID.Enabled = true;
            txt_Password.Enabled = true;
            cmbDatabases.Enabled = false;
            simpleBt_Backup.Enabled = false;
        }

        private void Frm_Backup_Load(object sender, EventArgs e)
        {
            simpleBt_Connect.Enabled = true;
            cmbDatabases.Enabled = true;
            simpleBt_Backup.Enabled = false;
        }

        private void simpleBt_Backup_Click(object sender, EventArgs e)
        {
            /*try
            {
                if (cmbDatabases.Text.CompareTo("") == 0)
                {
                    MessageBox.Show("Chọn database");
                    return;
                }
                conn = new SqlConnection(connectionString);
                conn.Open();
                sql = "BACKUP DATABASE" + cmbDatabases.Text + " TO DISK='" + txt_backup.Text + "\\" + cmbDatabases.Text + "-" + DateTime.Now.Ticks.ToString() + ".bak'";
                command = new SqlCommand(sql, conn);
                command.ExecuteNonQuery();
                MessageBox.Show("Backup thành công!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }*/
            if (txt_backup.Text == "")
            {
                MessageBox.Show("chưa chọn đường dẫn lưu");
                txt_backup.Focus();
            }
            else
            {
              
                if (File.Exists(txt_backup.Text))
                {
                    File.Delete(txt_backup.Text);
                }
                try
                {
                 
                    ketnoi();
                    string saoluu = "Backup Database QLDT to disk = '" + txt_backup.Text + "' ";
                    SqlCommand cmd = new SqlCommand(saoluu, conn);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Đã backup thành công ùi nhé!");
                    huyketnoi();
                }
                catch (Exception)
                {
                    MessageBox.Show("không thể  backup CSDL!", "Thông Báo !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void huyketnoi()
        {
            conn.Close();
            conn.Dispose();//giai fong tai nguyen 
            conn = null;//huy doi tương 
        }

        private void ketnoi()
        {
            string kn = @"Server=(local);Database=QLDT;Trusted_Connection=True;";
            try
            {
                conn = new SqlConnection(kn);
                conn.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("không thể kết nối tới CSDL!", "Thông Báo !", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void simpleBt_browse_Click(object sender, EventArgs e)
        {
            /*FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txt_backup.Text = dlg.SelectedPath;
            }
            simpleBt_Backup.Enabled = true;*/
            SaveFileDialog save = new SaveFileDialog();
            save.FileName = "QLDT.Bak"; //tên mặc định cho database 
            save.Filter = "File(*.bak)|*.bak";
            if (save.ShowDialog() == DialogResult.OK)
            {
                txt_backup.Text = save.FileName;
            }
            simpleBt_Backup.Enabled = true;


        }
    }
}